#!/bin/bash


# Setting PostgreSQL install path (postgresql_path) under ${HOME}/Applications
postgresql_path=${HOME}/Applications/PostgreSQL


while getopts "iu" opt; do  
  case $opt in  
    i)  
      # Clone the PostgreSQl source code
      git clone https://github.com/postgres/postgres.git
      cd postgres


      # Compile PostgreSQL and install it to postgresql_path
      ./configure --prefix=${postgresql_path} && make -j4 && make install
      cd ..
      rm -rf postgres

      # Init PostgreSQL's database cluster and create dir for log file
      mkdir ${postgresql_path}/data
      mkdir ${postgresql_path}/log
      ${postgresql_path}/bin/initdb --pgdata=${postgresql_path}/data --encoding='UTF-8' --no-locale --username=postgres


      # Add PostgreSQL's command line tools to PATH
      cp ~/.bash_profile ~/.bash_profile.before_postgres_install
      echo "" >> ~/.bash_profile
      echo "#Setting for PostgreSQL Command Line Tools from PostgreSQL install script" >> ~/.bash_profile
      echo "export PATH=\"${postgresql_path}/bin:\$PATH\" #From PostgreSQL install script" >> ~/.bash_profile


      # Alias "pg_ctl" to "pg_ctl -D $HOME/Applications/PostgreSQL/data -l $HOME/Applications/PostgreSQL/log/pg_server.log"
      echo "alias pg_ctl=\"pg_ctl -D ${postgresql_path}/data -l ${postgresql_path}/log/pg_server.log\" #From PostgreSQL install script" >> ~/.bash_profile
      echo "" >> ~/.bash_profile 


      # Source ~/.bash_profile
      source ~/.bash_profile


      echo "------------------------------------------------------------------------------------------------------------"
      echo "------------------------------------------------------------------------------------------------------------"
      echo "PostgreSQL was installed for your account on macOS"
      echo ""
      echo "PostgreSQL was installed to ${postgresql_path}"
      echo ""
      echo "PostgreSQL's database cluster was set to ${postgresql_path}/data"
      echo ""
      echo "PostgreSQL's log file wat set to ${postgresql_path}/log/pg_server.log"
      echo ""
      echo "\"pg_ctl\" was aliased to \"pg_ctl -D ${postgresql_path}/data -l ${postgresql_path}/log/pg_server.log\""
      echo "------------------------------------------------------------------------------------------------------------"
      echo "------------------------------------------------------------------------------------------------------------"
      echo "Now you can start PostgreSQL in terminal by run \"pg_ctl start\""
      echo ""
      echo "Then you can connect the PostgreSQL in terminal by run \"psql -U postgres -h 127.0.0.1 -p 5432\""
      echo ""
      echo "If you want to stop the PostgreSQL, you can stop it by run \"pg_ctl stop\" in ternimal"
      echo "------------------------------------------------------------------------------------------------------------"
      echo "------------------------------------------------------------------------------------------------------------"
      ;;  
    u)  
      rm -rf postgresql_path
      cp ~/.bash_profile ~/.bash_profile.before_postgres_uninstall
      sed -e '/PostgreSQL install script/d' ~/.bash_profile > ~/.bash_profile.temp
      mv ~/.bash_profile.temp ~/.bash_profile
      echo "Uninstall complete"
      ;;   
  esac  
done